//
//  DoctorTableViewCell.m
//  ybzj
//
//  Created by liyuan on 15/7/20.
//  Copyright (c) 2015年 liyuan. All rights reserved.
//

#import "DoctorTableViewCell.h"

@interface DoctorTableViewCell()
@property (weak, nonatomic) IBOutlet UILabel *photoLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UITextView *introduceTextView;
@property (weak, nonatomic) IBOutlet UILabel *opionionLabel;
@property (weak, nonatomic) IBOutlet UILabel *StoreLabel;
@property (weak, nonatomic) IBOutlet UIButton *StoreButton;
@property (weak, nonatomic) IBOutlet UIButton *appointButton;
@property (nonatomic) UIImageView *imageView;

@end

@implementation DoctorTableViewCell
@synthesize entity;
@synthesize imageView;


- (void)awakeFromNib {
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    
    //photolabel
    self.photoLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforPhotoLabel=[NSLayoutConstraint constraintWithItem:self.photoLabel
                                                                               attribute:NSLayoutAttributeTop
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.contentView
                                                                               attribute:NSLayoutAttributeTop
                                                                              multiplier:1.0f
                                                                                constant:width*32/640];
    NSLayoutConstraint *leftConstrainforPhotoLabel=[NSLayoutConstraint constraintWithItem:self.photoLabel
                                                                                attribute:NSLayoutAttributeLeading
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.contentView
                                                                                attribute:NSLayoutAttributeLeading
                                                                               multiplier:1.0f
                                                                                 constant:width*32/640];
    NSLayoutConstraint *heightConstrainforPhotoLabel=[NSLayoutConstraint constraintWithItem:self.photoLabel
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:nil
                                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                                 multiplier:1.0f
                                                                                   constant:width*72/640];
    NSLayoutConstraint *widthConstrainforPhotoLabel=[NSLayoutConstraint constraintWithItem:self.photoLabel
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:nil
                                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                                multiplier:1.0f
                                                                                  constant:width*72/640];
    topConstrainforPhotoLabel.active=YES;
    leftConstrainforPhotoLabel.active=YES;
    heightConstrainforPhotoLabel.active=YES;
    widthConstrainforPhotoLabel.active=YES;
    self.photoLabel.layer.cornerRadius=width*36/640;
    self.photoLabel.layer.masksToBounds=YES;
    self.photoLabel.backgroundColor=[UIColor redColor];
    self.imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width*72/640, width*72/640)];
    //self.imageView.image=[UIImage imageNamed:self.entity.photourl];
    [self.photoLabel addSubview:self.imageView];
    
    
    //namelabel
    self.nameLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforNameLabel=[NSLayoutConstraint constraintWithItem:self.nameLabel
                                                                              attribute:NSLayoutAttributeTop
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:self.contentView
                                                                              attribute:NSLayoutAttributeTop
                                                                             multiplier:1.0f
                                                                               constant:width*32/640];
    NSLayoutConstraint *leftConstrainforNameLabel=[NSLayoutConstraint constraintWithItem:self.nameLabel
                                                                               attribute:NSLayoutAttributeLeading
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.contentView
                                                                               attribute:NSLayoutAttributeLeading
                                                                              multiplier:1.0f
                                                                                constant:width*124/640];
    NSLayoutConstraint *heightConstrainforNameLabel=[NSLayoutConstraint constraintWithItem:self.nameLabel
                                                                                 attribute:NSLayoutAttributeHeight
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:nil
                                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                                multiplier:1.0f
                                                                                  constant:width*26/640];
    NSLayoutConstraint *widthConstrainforNameLabel=[NSLayoutConstraint constraintWithItem:self.nameLabel
                                                                                attribute:NSLayoutAttributeWidth
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:nil
                                                                                attribute:NSLayoutAttributeNotAnAttribute
                                                                               multiplier:1.0f
                                                                                 constant:width*200/640];
    topConstrainforNameLabel.active=YES;
    leftConstrainforNameLabel.active=YES;
    heightConstrainforNameLabel.active=YES;
    widthConstrainforNameLabel.active=YES;
   
    self.introduceTextView.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *topConstrainforIntroduceTextView=[NSLayoutConstraint constraintWithItem:self.introduceTextView
                                                                                      attribute:NSLayoutAttributeTop
                                                                                      relatedBy:NSLayoutRelationEqual
                                                                                         toItem:self.nameLabel
                                                                                      attribute:NSLayoutAttributeBottom
                                                                                     multiplier:1.0f
                                                                                       constant:0];
    NSLayoutConstraint *leftConstrainforIntroduceTextView=[NSLayoutConstraint constraintWithItem:self.introduceTextView
                                                                                       attribute:NSLayoutAttributeLeading
                                                                                       relatedBy:NSLayoutRelationEqual
                                                                                          toItem:self.contentView
                                                                                       attribute:NSLayoutAttributeLeading
                                                                                      multiplier:1.0f
                                                                                        constant:width*115/640];
   
    NSLayoutConstraint *widthConstrainforIntroduceTextView=[NSLayoutConstraint constraintWithItem:self.introduceTextView
                                                                                        attribute:NSLayoutAttributeWidth
                                                                                        relatedBy:NSLayoutRelationEqual
                                                                                           toItem:nil
                                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                                       multiplier:1.0f
                                                                                         constant:width*336/640];
    topConstrainforIntroduceTextView.active=YES;
    leftConstrainforIntroduceTextView.active=YES;
    //heightConstrainforIntroduceTextView.active=YES;
    widthConstrainforIntroduceTextView.active=YES;
    
    self.introduceTextView.editable=NO;
    self.introduceTextView.scrollEnabled=NO;
    // [self.introduceTextView setAttributedText:AttributedIntroduce];
    
    
    //optionion label
    self.opionionLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *bottomConstrainforOpionionLabel=[NSLayoutConstraint constraintWithItem:self.opionionLabel
                                                                                     attribute:NSLayoutAttributeBottom
                                                                                     relatedBy:NSLayoutRelationEqual
                                                                                        toItem:self.contentView
                                                                                     attribute:NSLayoutAttributeBottom
                                                                                    multiplier:1.0f
                                                                                      constant:0-width*15/640];
    NSLayoutConstraint *leftConstrainforOpionionLabel=[NSLayoutConstraint constraintWithItem:self.opionionLabel
                                                                                   attribute:NSLayoutAttributeLeading
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.contentView
                                                                                   attribute:NSLayoutAttributeLeading
                                                                                  multiplier:1.0f
                                                                                    constant:width*124/640];
    NSLayoutConstraint *heightConstrainforOpionionLabel=[NSLayoutConstraint constraintWithItem:self.opionionLabel
                                                                                     attribute:NSLayoutAttributeHeight
                                                                                     relatedBy:NSLayoutRelationEqual
                                                                                        toItem:nil
                                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                                    multiplier:1.0f
                                                                                      constant:width*18/640];
    NSLayoutConstraint *widthConstrainforOpionionLabel=[NSLayoutConstraint constraintWithItem:self.opionionLabel
                                                                                    attribute:NSLayoutAttributeWidth
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:nil
                                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                                   multiplier:1.0f
                                                                                     constant:width*140/640];
    bottomConstrainforOpionionLabel.active=YES;
    leftConstrainforOpionionLabel.active=YES;
    heightConstrainforOpionionLabel.active=YES;
    widthConstrainforOpionionLabel.active=YES;
    
    //store label
    self.StoreLabel.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *bottomConstrainforStoreLabel=[NSLayoutConstraint constraintWithItem:self.StoreLabel
                                                                                  attribute:NSLayoutAttributeBottom
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.contentView
                                                                                  attribute:NSLayoutAttributeBottom
                                                                                 multiplier:1.0f
                                                                                   constant:0-width*15/640];
    NSLayoutConstraint *leftConstrainforStoreLabel=[NSLayoutConstraint constraintWithItem:self.StoreLabel
                                                                                attribute:NSLayoutAttributeTrailing
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.contentView
                                                                                attribute:NSLayoutAttributeTrailing
                                                                               multiplier:1.0f
                                                                                 constant:width*264/640];
    NSLayoutConstraint *heightConstrainforStoreLabel=[NSLayoutConstraint constraintWithItem:self.StoreLabel
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:nil
                                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                                 multiplier:1.0f
                                                                                   constant:width*20/640];
    NSLayoutConstraint *widthConstrainforStoreLabel=[NSLayoutConstraint constraintWithItem:self.StoreLabel
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:nil
                                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                                multiplier:1.0f
                                                                                  constant:width*140/640];
    bottomConstrainforStoreLabel.active=YES;
    leftConstrainforStoreLabel.active=YES;
    heightConstrainforStoreLabel.active=YES;
    widthConstrainforStoreLabel.active=YES;
    
    
    self.StoreButton.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *rightConstrainforStoeButton=[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                                 attribute:NSLayoutAttributeTrailing
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:self.contentView
                                                                                 attribute:NSLayoutAttributeTrailing
                                                                                multiplier:1.0f
                                                                                  constant:0];
    NSLayoutConstraint *topConstrainforStoeButton=[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                               attribute:NSLayoutAttributeTop
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.contentView
                                                                               attribute:NSLayoutAttributeTop
                                                                              multiplier:1.0f
                                                                                constant:width*85/640];
    NSLayoutConstraint *heightConstrainforStoeButton=[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                                  attribute:NSLayoutAttributeHeight
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:nil
                                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                                 multiplier:1.0f
                                                                                   constant:width*88/640];
    NSLayoutConstraint *widthConstrainforStoeButton=[NSLayoutConstraint constraintWithItem:self.StoreButton
                                                                                 attribute:NSLayoutAttributeWidth
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                    toItem:nil
                                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                                multiplier:1.0f
                                                                                  constant:width*140/640];
    rightConstrainforStoeButton.active=YES;
    topConstrainforStoeButton.active=YES;
    heightConstrainforStoeButton.active=YES;
    widthConstrainforStoeButton.active=YES;
    
    
    self.appointButton.translatesAutoresizingMaskIntoConstraints=NO;
    NSLayoutConstraint *rightConstrainforAppointButton=[NSLayoutConstraint constraintWithItem:self.appointButton
                                                                                    attribute:NSLayoutAttributeTrailing
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:self.contentView
                                                                                    attribute:NSLayoutAttributeTrailing
                                                                                   multiplier:1.0f
                                                                                     constant:0];
    NSLayoutConstraint *topConstrainforAppointButton=[NSLayoutConstraint constraintWithItem:self.appointButton
                                                                                  attribute:NSLayoutAttributeTop
                                                                                  relatedBy:NSLayoutRelationEqual
                                                                                     toItem:self.contentView
                                                                                  attribute:NSLayoutAttributeTop
                                                                                 multiplier:1.0f
                                                                                   constant:width*172/640];
    NSLayoutConstraint *heightConstrainforAppointButton=[NSLayoutConstraint constraintWithItem:self.appointButton
                                                                                     attribute:NSLayoutAttributeHeight
                                                                                     relatedBy:NSLayoutRelationEqual
                                                                                        toItem:nil
                                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                                    multiplier:1.0f
                                                                                      constant:width*88/640];
    NSLayoutConstraint *widthConstrainforAppointButton=[NSLayoutConstraint constraintWithItem:self.appointButton
                                                                                    attribute:NSLayoutAttributeWidth
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:nil
                                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                                   multiplier:1.0f
                                                                                     constant:width*140/640];
    rightConstrainforAppointButton.active=YES;
    topConstrainforAppointButton.active=YES;
    heightConstrainforAppointButton.active=YES;
    widthConstrainforAppointButton.active=YES;
    
    //commet label
    UIImageView *opinoinImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, width*1/640, width*18/640, width*18/640)];
    opinoinImage.image=[UIImage imageNamed:@"discuss.ico"];
    [self.opionionLabel addSubview:opinoinImage];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.firstLineHeadIndent=width*25/640;
    NSMutableAttributedString *AttributedCommit=[[NSMutableAttributedString alloc]  initWithString:[NSString stringWithFormat:@"%d%@",self.entity.opioninNum,@" 评论"]];
    [AttributedCommit addAttribute:NSForegroundColorAttributeName
                             value:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1] range:NSMakeRange(0, [AttributedCommit length])];
    [AttributedCommit addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:width*20/640] range:NSMakeRange(0, [AttributedCommit length])];
    [AttributedCommit addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [AttributedCommit length])];
    [self.opionionLabel setAttributedText:AttributedCommit];
    [self.opionionLabel setTextAlignment:NSTextAlignmentLeft];
    
    UIImageView *storeImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, width*1/640, width*18/640, width*18/640)];
    storeImage.image=[UIImage imageNamed:@"heart.png"];
    [self.StoreLabel addSubview:storeImage];
    NSMutableAttributedString *AttributedStore=[[NSMutableAttributedString alloc]  initWithString:[NSString stringWithFormat:@"%d%@",self.entity.storeNum,@" 收藏"]];
    [AttributedStore addAttribute:NSForegroundColorAttributeName
                            value:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1] range:NSMakeRange(0, [AttributedStore length])];
    [AttributedStore addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:width*20/640] range:NSMakeRange(0, [AttributedStore length])];
    [AttributedStore addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [AttributedStore length])];
    [self.StoreLabel setAttributedText:AttributedStore];
    [self.StoreLabel setTextAlignment:NSTextAlignmentLeft];
    
    
    [self.StoreButton setTitleEdgeInsets:UIEdgeInsetsMake(0, width*60/640, 0, 0)];
    [self.StoreButton setTitle:@"收藏" forState:UIControlStateNormal];
    self.StoreButton.titleLabel.font = [UIFont systemFontOfSize:width*20/640];
    [self.StoreButton setTitleColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1] forState:UIControlStateNormal];
    UIImageView *storeButtonImage=[[UIImageView alloc] initWithFrame:CGRectMake(width*15/640, width*27/640, width*34/640, width*34/640)];
    storeButtonImage.image=[UIImage imageNamed:@"heart1.tiff"];
    [self.StoreButton addSubview:storeButtonImage];
    
    [self.appointButton setTitleEdgeInsets:UIEdgeInsetsMake(0, width*60/640, 0, 0)];
    [self.appointButton setTitle:@"预约" forState:UIControlStateNormal];
    self.appointButton.titleLabel.font = [UIFont systemFontOfSize:width*20/640];
    [self.appointButton setTitleColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1] forState:UIControlStateNormal];
    UIImageView *appointButtonImage=[[UIImageView alloc] initWithFrame:CGRectMake(width*15/640, width*27/640, width*34/640, width*34/640)];
    appointButtonImage.image=[UIImage imageNamed:@"plus1.tiff"];
    [self.appointButton addSubview:appointButtonImage];
}


-(void) setView{
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    self.imageView.image=[UIImage imageNamed:self.entity.photourl];
    NSMutableAttributedString *AttributedName=[[NSMutableAttributedString alloc]  initWithString:self.entity.name];
    [AttributedName addAttribute:NSForegroundColorAttributeName
                          value:[UIColor colorWithRed:79/255.0 green:79/255.0 blue:79/255.0 alpha:1] range:NSMakeRange(0, [self.entity.name length])];
    [AttributedName addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:width*26/640] range:NSMakeRange(0, [self.entity.name length])];
    [self.nameLabel setAttributedText:AttributedName];
    
    //textView
    NSMutableAttributedString *AttributedIntroduce=[[NSMutableAttributedString alloc]  initWithString:self.entity.introduceText];
    [AttributedIntroduce addAttribute:NSForegroundColorAttributeName
                                value:[UIColor colorWithRed:127/255.0 green:127/255.0 blue:127/255.0 alpha:1] range:NSMakeRange(0, [self.entity.introduceText length])];
    [AttributedIntroduce addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:width*26/640] range:NSMakeRange(0, [self.entity.introduceText length])];
    
    CGRect TextViewRect=[AttributedIntroduce boundingRectWithSize:CGSizeMake(width*336/640, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine context:nil];
     NSLayoutConstraint *heightConstrainforIntroduceTextView=[NSLayoutConstraint constraintWithItem:self.introduceTextView
                                                                                         attribute:NSLayoutAttributeHeight
                                                                                          relatedBy:NSLayoutRelationEqual
                                                                                             toItem:nil
                                                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                                                         multiplier:1.0f
                                                                                          constant:TextViewRect.size.height ];
     heightConstrainforIntroduceTextView.active=YES;
    [self.introduceTextView setAttributedText:AttributedIntroduce];
    
    }



-(void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);//线条颜色
    CGContextMoveToPoint(context, rect.size.width*124/640, rect.size.height);
    CGContextAddLineToPoint(context, rect.size.width,rect.size.height);
    CGContextStrokePath(context);
}
@end
